package com.example.smart_city_0829.activity.pet_hos;

import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.smart_city_0829.R;
import com.example.smart_city_0829.base.BaseActivity;
import com.example.smart_city_0829.entity.Base;
import com.example.smart_city_0829.entity.petHos.PetDoc;
import com.example.smart_city_0829.retrofit.NetCallBack;
import com.example.smart_city_0829.utils.Constant;
import com.google.gson.reflect.TypeToken;

import java.util.HashMap;
import java.util.Objects;
import java.util.Timer;
import java.util.TimerTask;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Response;

/**
 * 问诊页面
 */
public class InterrogationActivity extends BaseActivity {

    private PetDoc.RowsBean doctor;

    private ImageView imageView;
    private TextView nameTv;
    private TextView jobNameTv;
    private TextView practiceNoTv;
    private EditText quesEt;
    private ImageButton postIm;
    private Button postBt;

    private Timer timer;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_interrogation;
    }

    @Override
    protected void initView() {
        imageView = (ImageView) findViewById(R.id.imageView);
        nameTv = (TextView) findViewById(R.id.name_tv);
        jobNameTv = (TextView) findViewById(R.id.jobName_tv);
        practiceNoTv = (TextView) findViewById(R.id.practiceNo_tv);
        quesEt = (EditText) findViewById(R.id.ques_et);
        postIm = (ImageButton) findViewById(R.id.post_im);
        postBt = (Button) findViewById(R.id.post_bt);
    }

    @Override
    protected void initData() {
        final String gsonDoc = getIntent().getStringExtra("doc");
        doctor = (PetDoc.RowsBean) gson.fromJson(gsonDoc,new TypeToken<PetDoc.RowsBean>() {}.getType());

        Glide.with(context)
                .load(Constant.BASE_URL + doctor.getAvatar())
                .into(imageView);
        nameTv.setText(String.valueOf(doctor.getName()));
        practiceNoTv.setText(String.valueOf(doctor.getPracticeNo()));
        jobNameTv.setText(String.valueOf(doctor.getJobName()));

    }

    @Override
    protected void initEvent() {
        postBt.setOnClickListener(v -> {
            postBt.setEnabled(false);
            showLoadingDialog(null,null, ProgressDialog.STYLE_SPINNER);
            final HashMap<String, Object> params = new HashMap<>();
            params.put("doctorId",doctor.getId());
            params.put("question",quesEt.getText().toString());
            //TODO 提交图片！！
//            params.put("imageUrls",)
            api.postInquiryQuery(
                    RequestBody.create(gson.toJson(params), MediaType.parse("application/json"))
            ).enqueue(new NetCallBack<Base>() {
                @Override
                public void onResponse(Call<Base> call, Response<Base> response) {
                    postBt.setEnabled(true);
                    progressDialog.dismiss();
                    if (response.body() == null) return;

                    //请求成功之后跳转至主页面
                    if (response.body().getCode() == 200){
                        showToastShort("3秒后跳转至医院主页");
                        timer = new Timer();
                        timer.schedule(new TimerTask() {
                            @Override
                            public void run() {
                                popActivityStack(2);
                            }
                        },3000);
                    }else {

                    }
                    showToastShort(response.body().getMsg());
                }
            });
        });
    }

    @Override
    protected String getActionBarTitle() {
        return "问诊";
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null){
            timer.cancel();
        }
    }
}